import os
from fastapi import applications
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html


def swagger_monkey_patch(*args, **kwargs):
    return get_swagger_ui_html(
        *args, **kwargs,
        swagger_js_url='/local/js/swagger-ui-bundle.js',
        swagger_css_url='/local/css/swagger-ui.css',
        swagger_favicon_url='/local/img/favicon.png'
    )


def redoc_monkey_patch(*args, **kwargs):
    return get_redoc_html(
        *args, **kwargs,
        redoc_js_url="/local/js/redoc.standalone.js",
        redoc_favicon_url="/local/img/favicon.png",
        with_google_fonts=False
    )


def mount_local_static(app):
    """使用本地缓存static
    展示swagger"""
    applications.get_swagger_ui_html = swagger_monkey_patch
    applications.get_redoc_html = redoc_monkey_patch
    app.mount('/local', StaticFiles(directory=os.path.join(os.path.dirname(__file__), 'local')),
              name='local')
